﻿#include "algorithm_base.h"

algorithm_base::algorithm_base()
{
	m_nThreadState = 1;
	m_ThreadPool.start(new _Thread(this));
}


algorithm_base::~algorithm_base()
{
	
}


void algorithm_base::stop()
{
	while (!this->m_ImageQueue.isEmpty())
	{
		QThread::msleep(1);
	}

	m_nThreadState = 0;
}


/// <summary>
/// 
/// </summary>
/// <param name="data"></param>
int algorithm_base::pustImage(IMG_DATA data)
{
	
	QSharedPointer<IMG_DATA> pData = QSharedPointer<IMG_DATA>(new IMG_DATA);
	
	pData->strTopicName = data.strTopicName;
	pData->strName = data.strName;
	pData->image = data.image;
	pData->nDispId = data.nDispId;
	pData->strStationName = data.strStationName;
	pData->strStationId = data.strStationId;
	pData->strCameraName = data.strCameraName;
	pData->strCameraId = data.strCameraId;
	pData->strPictureName = data.strPictureName;
	pData->strUrl = data.strUrl;

	QWriteLocker locker(&m_ImageQueue_lock_);
	m_ImageQueue.enqueue(pData);

	qWarning() << data.strName << "buffer size " << m_ImageQueue.size();

	return m_ImageQueue.size();
}


QImage algorithm_base::urlToPixmap(QString url)
{
	QNetworkAccessManager manager;
	QEventLoop loop;

	QNetworkReply* reply = manager.get(QNetworkRequest(QUrl(url)));

	// 等待网络请求完成
	QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
	loop.exec();

	// 处理错误情况
	if (reply->error() != QNetworkReply::NoError) 
	{
		qWarning() << "Failed to download image:" << reply->errorString();
		return QImage();
	}

	QByteArray data = reply->readAll();
	QPixmap pixmap;
	pixmap.loadFromData(data);

	QImage img = pixmap.toImage();

	return img;
}

void algorithm_base::saveImage(IMG_DATA data)
{
	QString strDate = QDateTime::currentDateTime().toString("yyyy-MM-dd");

	//if (data.image.isNull() )
	{
		return;
	}

	QString strPath = g_strSaveImageDir + "//" + data.strName + "//" + strDate + "//" +
		data.strStationName + "//" + data.strPictureName;
	DirExistEx(strPath);
	data.image.save(strPath);
}


void algorithm_base::pushKafka(QString strTopc, QString strJson, QString strStationId)
{
	mutex.lock();
	ExampleProducer producer(KAFKA_IP, strTopc.toUtf8().data() );
#ifdef _WIN32
    producer.produce(strJson.toUtf8().data());
#endif
	mutex.unlock();
}


void algorithm_base::saveImage(cv::Mat mat, QString strPrjName, IMG_DATA data, int nState)
{
	QString strPictureName = data.strPictureName;

	QString strDate = QDateTime::currentDateTime().toString("yyyy-MM-dd");
	QString strTime = QDateTime::currentDateTime().toString("MM-dd_hh-mm-ss");

	QString strState;

	switch (nState)
	{
	case -1:strState = ""; break;
	case 0:strState = "未检出"; break;
	case 1:strState = "检出"; break;
	case 2:strState = "检出"; break;
	case 3:strState = "检出"; break;
	case 4:strState = "检出"; break;
	case 5:strState = "检出"; break;

	default:strState = "检出"; break;
	}

	QString str;

	QString strID = QString::number(data.nDispId);
	str = g_strReleaseImageDir + "//" + data.strName + "//" + strID + "_" + strState + "_" + strPrjName + "//" + strDate + "//" + data.strStationId + "_" + data.strStationName + "//" + strTime + "_" + strPictureName;

	DirExistEx(str);
	cv::imwrite(str.toLocal8Bit().data(), mat);

}


/// <summary>
/// 线程函数
/// </summary>
void _Thread::run()
{
	while (m_pDlg != nullptr && m_pDlg->m_nThreadState.load(std::memory_order_release))
	{
		QSharedPointer<IMG_DATA> pData = nullptr;

		{
			QReadLocker locker(&m_pDlg->m_ImageQueue_lock_);
			if (!m_pDlg->m_ImageQueue.isEmpty())
			{
				pData = m_pDlg->m_ImageQueue.dequeue();
			}
		}

		if (pData)
		{
			try
			{
				int nBufferSize = 0;
				nBufferSize = m_pDlg->m_ImageQueue.size();
				m_pDlg->execute(pData, nBufferSize);
			}
			catch (...)
			{
				qWarning() << "catch....";
			}
		}
		else
		{
			QThread::msleep(1);
		}
	}
	return;
}
